package com.realgodo.site.service.relation;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.hibernate.DefaultEntityManager;

import com.realgodo.site.entity.relation.FriendConfig;

@Service
@Transactional
public class FriendConfigManager extends DefaultEntityManager<FriendConfig, Long> {

	@Autowired
	protected SessionFactory sessionFactory;
	
	@Autowired
	private FriendManager friendManager;
	
	public Page<FriendConfig> find(int pageSize,Map<String,Object> param){
		Page<FriendConfig> page=new Page<FriendConfig>(pageSize);
		Criteria c = sessionFactory.getCurrentSession().createCriteria(FriendConfig.class);
		if(param==null){
			page.setPageNo(1);
			return this.entityDao.findByCriteria(page, c);
		}
		
		page.setPageNo(super.isParamNotNull(param, Page.PAGE)?Integer.valueOf((String)getParamValue(param,  Page.PAGE)):1);//查询页数
		if(isParamNotNull(param, "receiverId")){
			c.createAlias("friend", "friend").createAlias("friend.receiver", "receiver");
			c.add(Restrictions.eq("receiver.id", Long.valueOf((String)getParamValue(param, "receiverId"))));
			c.add(Restrictions.eq("onwer.id", Long.valueOf((String)getParamValue(param, "receiverId"))));
		}else if(isParamNotNull(param, "spaceId")){
			c.add(Restrictions.eq("onwer.id", Long.valueOf((String)getParamValue(param, "spaceId"))));
		}
		
		if(isParamNotNull(param, "status")){
			if(!isParamNotNull(param, "receiverId")){
				c.createAlias("friend", "friend");
			}
			c.add(Restrictions.eq("friend.status", Integer.valueOf((String)getParamValue(param, "status"))));
		}
		if(isParamNotNull(param, "name")){
			c.createAlias("onwer", "onwer");
			c.add(Restrictions.like("onwer.name", (String)getParamValue(param, "name"),MatchMode.ANYWHERE));
		}
		if(isParamNotNull(param, "isTop")){
			c.add(Restrictions.eq("isTop", Integer.valueOf((String)getParamValue(param, "isTop")).intValue()));
		}
		
		if(isParamNotNull(param, "catalogId")){
			c.add(Restrictions.eq("catalog.id", Long.valueOf((String)getParamValue(param, "catalogId"))));
		}else if(isParamNotNull(param, "topCatalogId")){
			c.createCriteria("catalog", "catalog");
			c.add(Restrictions.eq("catalog.parent.id", Long.valueOf((String)getParamValue(param, "topCatalogId"))));
		}
		c.addOrder(Order.desc("id"));
		return this.entityDao.findByCriteria(page, c);
	}
	
	@SuppressWarnings("unchecked")
	public List<FriendConfig> find(Map<String,Object> param){
		Criteria c = sessionFactory.getCurrentSession().createCriteria(FriendConfig.class);
		if(param==null){
			return new ArrayList<FriendConfig>();
		}
		
		if(isParamNotNull(param, "spaceId")){
			c.add(Restrictions.eq("onwer.id", Long.valueOf((String)getParamValue(param, "spaceId"))));
		}
		
		if(isParamNotNull(param, "name")){
			c.createAlias("onwer", "onwer");
			c.add(Restrictions.like("onwer.name", (String)getParamValue(param, "name"),MatchMode.ANYWHERE));
		}
		
		if(isParamNotNull(param, "isTop")){
			c.add(Restrictions.eq("isTop", Integer.valueOf((String)getParamValue(param, "isTop")).intValue()));
		}
		
		if(isParamNotNull(param, "status")){
			c.createAlias("friend", "friend");
			c.add(Restrictions.eq("friend.status", Integer.valueOf((String)getParamValue(param, "status"))));
		}
		
		if(isParamNotNull(param, "catalogId")){
			c.add(Restrictions.eq("catalog.id", Long.valueOf((String)getParamValue(param, "catalogId"))));
		}else if(isParamNotNull(param, "topCatalogId")){
			c.createCriteria("catalog", "catalog");
			c.add(Restrictions.eq("catalog.parent.id", Long.valueOf((String)getParamValue(param, "topCatalogId"))));
		}
		c.addOrder(Order.desc("id"));
		return c.list();
	}
	
	@SuppressWarnings("unchecked")
	public int getTopsCount(Long spaceId){
		Criteria c = sessionFactory.getCurrentSession().createCriteria(FriendConfig.class);
		ProjectionList projectionList=Projections.projectionList();//映射条件
		projectionList.add(Projections.property("id").as("id"));//发出者id
		c.add(Restrictions.eq("onwer.id", spaceId));
		c.add(Restrictions.eq("isTop", FriendConfig.IS));
		c.setProjection(projectionList);
		List result=c.list();
		return result==null?0:result.size();
	}
	
	/**移出朋友，并从对方的朋友删除记录*/
	public boolean remove(Long friendConfigId){
		if(friendConfigId==null||DAFAULT_LONG_NULL_VALUE.equals(friendConfigId)){
			return false;
		}
		FriendConfig friendConfig=this.get(friendConfigId);
		Long friendId=friendConfig.getFriend().getId();
		
		//移出朋友，并从对方的朋友删除记录
		sessionFactory.getCurrentSession()
		.createQuery("delete from FriendConfig o where o.friend.id = ?")
		.setParameter(0, friendId)
		.executeUpdate();
		//删除关系
		friendManager.delete(friendId);
//		sessionFactory.getCurrentSession()
//		.createQuery("delete from Friend o where o.id = ?")
//		.setParameter(0, friendId)
//		.executeUpdate();
//		sessionFactory.getCurrentSession()
//		.createQuery("delete from Relation o where o.id = ?")
//		.setParameter(0, friendId)
//		.executeUpdate();
		return true;
	}
}
